package com.wjp.banquet.common.config;

import com.wjp.banquet.common.filter.CrosFilter;
import com.wjp.banquet.common.filter.LoginCheckFilter;
import com.wjp.banquet.common.filter.RateLimiterFilter;
import com.wjp.banquet.service.UserService;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * @Author 王君平
 * @Date 2023/11/17 8:26
 * @Description
 */
@Configuration
public class FilterConfig {

    @Bean
    public FilterRegistrationBean crosFilter() {
        FilterRegistrationBean bean = new FilterRegistrationBean();
        bean.setFilter(new CrosFilter());
        bean.addUrlPatterns("/*");
        bean.setOrder(10);
        return bean;
    }

    @Bean
    public FilterRegistrationBean rateLimiterFilter() {
        FilterRegistrationBean bean = new FilterRegistrationBean();
        bean.setFilter(new RateLimiterFilter());
        bean.addUrlPatterns("/*");
        bean.setOrder(20);
        return bean;
    }

    @Bean
    public FilterRegistrationBean loginFilter(
            UserService userService
    ) {
        FilterRegistrationBean bean = new FilterRegistrationBean();
        bean.setFilter(new LoginCheckFilter(
                userService
        ));
        bean.addUrlPatterns("/*");
        bean.setOrder(30);
        return bean;
    }


}
